home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #2 / Ham Radio 2000 - Volume 2.iso / HAMV2 / TCP_IP / TNOS230S / COLOR.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-30  |  7.9 KB  |  435 lines

  1. #include "global.h"
  2. #include "ctype.h"
  3. #include "color.h"
  4. #include "socket.h"
  5. #include "session.h"
  6. #ifdef MSDOS
  7. #include "conio.h"
  8. #else
  9. #define COLOR_BLACK    0
  10. #define COLOR_RED    1
  11. #define COLOR_GREEN    2
  12. #define COLOR_YELLOW    3
  13. #define COLOR_BLUE    4
  14. #define COLOR_MAGENTA    5
  15. #define COLOR_CYAN    6
  16. #define COLOR_WHITE    7
  17. #endif
  18.  
  19. #if !defined(_lint)
  20. static char rcsid[] OPTIONAL = "$Id: color.c,v 1.17 1997/07/31 00:44:20 root Exp root $";
  21. #endif
  22.  
  23. #ifdef DEMO
  24. #define tprintf printf
  25. #define tputc putchar
  26.  
  27. static unsigned char last[2];
  28.  
  29. void
  30. tflush ()
  31. {
  32.     fflush (stdout);
  33. }
  34. #else
  35. #include "usock.h"
  36.  
  37. static void myusflush (int s);
  38.  
  39. static void
  40. myusflush (s)
  41. int s;
  42. {
  43. register struct usock *up;
  44.  
  45.     if((up = itop(s)) == NULLUSOCK)
  46.         return;
  47.  
  48.     if(up->obuf == NULLBUF)
  49.         return;
  50.     if (len_p (up->obuf) < 220)
  51.         return;
  52.     usflush (s);
  53. }
  54. #endif
  55.  
  56. static int getmycolor (unsigned char color);
  57. static int getone (FILE *fp);
  58. static void getkeyword (unsigned char *input,FILE *fp);
  59. static int cmdmatch (unsigned char *input);
  60. static int blinking (unsigned char c);
  61. static int bold (unsigned char c);
  62. #ifdef MSDOS
  63. extern int chksession (struct session *sp);
  64. #endif
  65. int getcolor (char *str);
  66. int docolor (int argc, char *argv[], void *p);
  67. void setscreens (int back, int fore, int clr);
  68. extern void displayStatLine (int offset, int phase, int onlymarquee);
  69.  
  70.  
  71. #ifdef MSDOS
  72. int SYSback = 40, SYSfore = 37;
  73. static int backs[8] = {0, 4, 2, 0, 1, 5, 3, 7};
  74. static int fores[8] = {0, 4, 2, 0, 1, 5, 3, 7};
  75. #else
  76. int SYSback = COLOR_BLUE, SYSfore = COLOR_WHITE;
  77. #endif
  78. extern int STATLINE;
  79.  
  80. static char nope[] = "Color '%s' unknown!\nAvailable colors: black, red, green, cyan, blue, magenta, and white\n";
  81.  
  82.  
  83.  
  84. int
  85. colorprintf (char *escapeflag, char usecolor, const unsigned char *str)
  86. {
  87. unsigned char c;
  88. int foundone = 0;
  89.  
  90.  
  91.     while ((c = *str++) != 0)    {
  92.         if (c == 0x1b)    {    /* escape sequence */
  93.             if (usecolor)    {
  94.                 foundone = 1;
  95. #ifdef DEMO
  96.                 tflush ();
  97. #else
  98.                 myusflush(Curproc->output);
  99. #endif
  100.             }
  101.             if (escapeflag)
  102.                 *escapeflag = 1;
  103.         }
  104.         else if (escapeflag && *escapeflag)    {
  105.             if (!isdigit(c) && c != ';' && c != '[')
  106.                 *escapeflag = 0;
  107.         }
  108.         if (!escapeflag || (escapeflag && !*escapeflag) || usecolor)    {
  109.             tputc (c);
  110.         }
  111.     }
  112.     return (foundone);
  113. }
  114.  
  115.  
  116. static int
  117. getmycolor (unsigned char color)
  118. {
  119. int retval = 30;
  120.  
  121.     switch (color)    {
  122.         case '1': case '9':    retval += 4;    break;
  123.         case '2': case 'A':    retval += 2;    break;
  124.         case '3': case 'B':    retval += 6;    break;
  125.         case '4': case 'C':    retval += 1;    break;
  126.         case '5': case 'D':    retval += 5;    break;
  127.         case '6': case 'E':    retval += 3;    break;
  128.         case '7': case 'F':    retval += 7;    break;
  129.         default:        break;
  130.     }
  131.     return (retval);
  132. }
  133.  
  134.  
  135. static int
  136. getone (fp)
  137. FILE *fp;
  138. {
  139. int c;
  140.  
  141.     c = getc (fp);
  142.     if (feof (fp))
  143.         c = 0;
  144.     return (c);
  145. }
  146.  
  147.  
  148. static void
  149. getkeyword (input, fp)
  150. unsigned char *input;
  151. FILE *fp;
  152. {
  153. unsigned char c;
  154. int k;
  155.  
  156.     do    {
  157.         for (k = 0; k < 15; k++)    {
  158.             input[k] = c = uchar(getone (fp));
  159.             if (!c || (c == '@'))
  160.                 break;
  161.         }
  162.         input[k] = 0;
  163.         if (!c)
  164.             k++;
  165.     } while (!k);
  166. }
  167.  
  168.  
  169. void
  170. colorcls ()
  171. {
  172. #ifdef DEMO
  173.     tflush ();
  174. #else
  175.     myusflush(Curproc->output);
  176. #endif
  177.     tprintf ("\033[2J");
  178. }
  179.  
  180.  
  181. static int
  182. cmdmatch (input)
  183. unsigned char *input;
  184. {
  185. int retval = 0;
  186.  
  187.     if (!strcmp ("CLS", (char *) input))    {
  188.         colorcls();
  189.         retval = 1;
  190.     }
  191.     return (retval);
  192. }
  193.  
  194.  
  195. static int
  196. blinking (unsigned char c)
  197. {
  198.     return ((c > '7') ? 1 : 0);
  199. }
  200.  
  201.  
  202. static int
  203. bold (unsigned char c)
  204. {
  205.     return ((c > '7') ? 1 : 0);
  206. }
  207.  
  208.  
  209. int
  210. colorchange (input, last)
  211. register const char *input;
  212. register char *last;
  213. {
  214. int retval = 0, putone = 0, temp, putall = 0;
  215.  
  216.  
  217.     if (!input || !last)
  218.         return (retval);
  219.     if (!strncmp (input, last, 2))
  220.         return (retval);
  221.     if ((strlen(input) == 2) && (isxdigit (input[0])) && (isxdigit (input[1])))    {
  222. #ifdef DEMO
  223.         tflush ();
  224. #else
  225.         myusflush(Curproc->output);
  226. #endif
  227.         tprintf ("\033[");
  228.         retval = 2;
  229.         if (!*last)
  230.             putall++;
  231.         if (putall || (blinking(uchar(*last)) && !blinking(uchar(*input))) || (bold (uchar(last[1])) && !bold(uchar(input[1])))) {
  232.             tputc ('0');
  233.             retval++;
  234.             putone++;
  235.             putall++;
  236.         }
  237.         if (blinking(uchar(*input)))    {
  238.             tprintf ("%s5", putone ? ";" : "");
  239.             putone++;
  240.             retval++;
  241.         }
  242.         temp = getmycolor(uchar(*input));
  243.         if (putall || temp != getmycolor (uchar(*last)))        {
  244.             tprintf ("%s%d", putone ? ";" : "", temp + 10);
  245.             retval += (2 + putone);
  246.             putone++;
  247.         }
  248.         if (putall || (input[1] != last[1]))    {
  249.             tprintf ("%s%s%d", putone ? ";" : "", (input[1] > '7') ? "1;" : "",
  250.                 getmycolor (uchar(input[1])));
  251.             retval += (putone + 2 + ((input[1] > 7) ? 2 : 0));
  252.         }
  253.         tputc ('m');
  254.         retval += 1;
  255.         memcpy (last, input, 2);
  256.     }
  257.     return (retval);
  258. }
  259.  
  260.  
  261. void
  262. colorfile (filenm, last)
  263. const char *filenm;
  264. char *last;
  265. {
  266. FILE *fp;
  267. unsigned char c, input[16];
  268. char invalid = 0;
  269.  
  270.     if ((fp = fopen (filenm, "rb")) == NULLFILE)
  271.         return;
  272.     while (!feof (fp))    {
  273.         if ((c = (unsigned char) getone (fp)) == '\0')
  274.             continue;
  275.         if (c == 0x1b)
  276.             invalid = 1;
  277.         if (c == '@')    {
  278.             invalid = 0;
  279.             getkeyword (input, fp);
  280.             if (!colorchange ((char *) input, last))
  281.                 if (!cmdmatch (input))
  282.                     tprintf ("@%s@", input);
  283.         } else
  284. #ifndef DEMO
  285.             if ((c != 0x0d) && (c != 0x1a))
  286. #endif
  287.                 tputc (c);
  288.     }
  289.     (void) fclose (fp);
  290.     if (invalid)
  291.         last[0] = 0;
  292. }
  293.  
  294.  
  295.  
  296. int
  297. getcolor (char *str)
  298. {
  299. #ifdef MSDOS
  300. char *ptr;
  301.  
  302.     for (ptr = str; *ptr; ptr++)
  303.         *ptr = (char) tolower (*ptr);
  304.     switch (*str) {
  305.         case 'r':    return 31;
  306.         case 'g':    return 32;
  307.         case 'm':    return 35;
  308.         case 'c':    return 36;
  309.         case 'w':    return 37;
  310.         case 'b':    if (str[1] != 'l')
  311.                     return -1;
  312.                 switch (str[2]) {
  313.                     case 'a':    return 30;
  314.                     case 'u':    return 34;
  315.                     default:    return 0;
  316.                 }
  317.         default:    return -1;
  318.     }
  319. #else
  320.     if (strcasecmp(str, "black") == 0)
  321.         return COLOR_BLACK;
  322.     if (strcasecmp(str, "red") == 0)
  323.         return COLOR_RED;
  324.     if (strcasecmp(str, "yellow") == 0)
  325.         return COLOR_YELLOW;
  326.     if (strcasecmp(str, "green") == 0)
  327.         return COLOR_GREEN;
  328.     if (strcasecmp(str, "blue") == 0)
  329.         return COLOR_BLUE;
  330.     if (strcasecmp(str, "magenta") == 0)
  331.         return COLOR_MAGENTA;
  332.     if (strcasecmp(str, "cyan") == 0)
  333.         return COLOR_CYAN;
  334.     if (strcasecmp(str, "white") == 0)
  335.         return COLOR_WHITE;
  336.     return -1;
  337. #endif
  338. }
  339.  
  340.  
  341.  
  342. /* Set terminal colors */
  343. int
  344. docolor (int argc OPTIONAL, char *argv[], void *p OPTIONAL)
  345. {
  346. int temp;
  347. int back, fore;
  348. #ifdef MSDOS
  349. struct session *sp = NULLSESSION;
  350. #endif
  351.  
  352.     temp = getcolor (argv[1]);
  353.     if (temp == -1)
  354.         tprintf (nope, argv[1]);
  355.     else {
  356. #ifdef MSDOS
  357.         back = temp + 10;
  358. #else
  359.         back = temp;
  360. #endif
  361.         temp = getcolor (argv[2]);
  362.         if (temp == -1)
  363.             tprintf (nope, argv[2]);
  364.         else {
  365.             fore = temp;
  366. #ifdef MSDOS
  367.             if (argc > 3) {
  368.                 temp = atoi (argv[3]);
  369.                 if (temp) {
  370.                     sp = &Sessions[temp];
  371.                     if (chksession (sp))
  372.                         sp->screen->attr = (((backs[back - 40] << 4) + (fores[fore - 30])) | 0x80);    /*lint !e701 !e734 */
  373.                 }
  374.             } else
  375. #endif
  376.             {
  377.                 SYSback = back;
  378.                 SYSfore = fore;
  379. #ifdef MSDOS
  380.                 setscreens (0, 0, 1);
  381. #else
  382.                 setscreens (0, 0, 0);
  383.                 textrefresh ();
  384. #endif
  385.             }
  386.         }
  387.     }
  388.     return 0;
  389. }
  390.  
  391.  
  392.  
  393. void
  394. setscreens (int back OPTIONAL, int fore OPTIONAL, int clr OPTIONAL)
  395. {
  396. int resetstatline = 0;
  397.  
  398.     if (!back && !fore)    {    /* if both black, use system colors */
  399.         back = SYSback;
  400.         fore = SYSfore;
  401. #ifdef MSDOS
  402.         back = backs[back - 40];
  403.         fore = fores[fore - 30];
  404. #endif
  405.         resetstatline = 1;
  406.     }
  407.  
  408.     textbackground (back);
  409.     textcolor (fore);
  410.     textattr ((back << 4) | fore);        /*lint !e701 */
  411.  
  412.     if (clr)    {
  413.         clrscr ();
  414.         if (resetstatline && STATLINE)    {
  415.             displayStatLine (1, 1, 0);
  416.             displayStatLine (-1, 1, 0);
  417.         }
  418.     }
  419. }
  420.  
  421.  
  422. #ifdef DEMO
  423. void
  424. main (argc, argv)
  425. int argc;
  426. char *argv[];
  427. {
  428.     last[0] = last[1] = 0;
  429.     if (argc == 1)
  430.         exit (0);
  431.     colorfile (argv[1], last);
  432. }
  433. #endif
  434.  
  435.